package com.qqt.csr.im.socketio.utils;

import com.corundumstudio.socketio.SocketIOClient;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SocketIOUtil {

    /**
     * 关闭老的连接
     * @param newClient
     * @param key
     */
    public static void tryCloseOldSocket(SocketIOClient newClient, String key) {
        SocketIOClient oldClient = SocketIOLocalCacheUtil.getClient(key);
        if (oldClient != null && !oldClient.equals(newClient)) {
            try {
                oldClient.disconnect();
            } catch (Throwable throwable) {
                log.error("尝试关闭老socketIO异常，key:{}", key, throwable);
            }
        }
    }

    /**
     * 关闭老的连接
     * @param key
     */
    public static void tryCloseOldSocket(String key) {
        SocketIOClient oldClient = SocketIOLocalCacheUtil.getClient(key);
        if (oldClient != null) {
            try {
                oldClient.disconnect();
            } catch (Throwable throwable) {
                log.error("尝试关闭老socketIO异常，key:{}", key, throwable);
            }
        }
    }

    /**
     * 关闭老的连接
     * @param socketIOClient
     */
    public static void tryCloseOldSocket(SocketIOClient socketIOClient) {
        if (socketIOClient != null) {
            try {
                socketIOClient.disconnect();
            } catch (Throwable throwable) {
                log.error("尝试关闭老socketIO异常", throwable);
            }
        }
    }
}
